local skel = fk.CreateSkill {
  name = "lb__zhenjin",
}

Fk:loadTranslationTable {
  ["lb__zhenjin"] = "震金",
  [":lb__zhenjin"] =
      "你可以弃置一张装备牌或失去1点体力以视为使用【杀】，目标角色须执行你未选择的一项，否则此【杀】额外结算一次。<br>" ..
      "背水：你获得X个“<b>斗志</b>”（X为体力值小于你的角色数）。",

  ["#lb__zhenjin"] = "震金：你可以选择一项视为使用【杀】，或选择“背水”获得斗志",
  ["#lb__zhenjin_ask"] = "震金：你可以%arg，视为使用【杀】",

  ["#lb__zhenjin_lose"] = "震金：你须失去1点体力，否则 %arg 额外结算一次",
  ["#lb__zhenjin_equip"] = "震金：你须弃置一张装备牌，否则 %arg 额外结算一次",
  ["#lb__zhenjin_hp"] = "失去1点体力",
  ["#lb__zhenjin_dis"] = "弃置一张装备牌",
  ["#lb__zhenjin_bs"] = "背水：获得等于体力值小于你的角色数个“<b>斗志</b>”",

  ["@lb__douzhi"] = "斗志",

  ["$lb__zhenjin1"] = "哼，现在才开始。",
  ["$lb__zhenjin2"] = "见识下火花四溅吧！",
  ["$lb__zhenjin3"] = "这一拳————送你回家！",
}


skel:addEffect("viewas", {
  mute = true,
  pattern = "slash",
  prompt = function(self, player, selected_cards, selected)
    if self.interaction.data and self.interaction.data ~= "#lb__zhenjin_bs" then
      return "#lb__zhenjin_ask:::" .. Fk:translate(self.interaction.data)
    end
    return "#lb__zhenjin"
  end,
  interaction = function(self, player)
    local all = { "#lb__zhenjin_dis", "#lb__zhenjin_hp", "#lb__zhenjin_bs" }
    local ask = table.simpleClone(all)
    if not table.find(player:getCardIds("he"), function(id)
          return Fk:getCardById(id).type == Card.TypeEquip and not player:prohibitDiscard(id)
        end) then
      ask = { "#lb__zhenjin_hp" }
    end
    return UI.ComboBox {
      choices = ask,
      all_choices = all,
    }
  end,
  card_filter = function(self, player, to_select, selected, selected_targets)
    if self.interaction.data ~= "#lb__zhenjin_hp" then
      return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip and
      not player:prohibitDiscard(to_select)
    end
    return false
  end,
  view_as = function(self, player, cards)
    local card = Fk:cloneCard("slash")
    card.skillName = skel.name
    if self.interaction.data ~= "#lb__zhenjin_hp" then
      if #cards > 0 then
        card:addFakeSubcards(cards)
      else
        return
      end
    end
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local list = { "#lb__zhenjin_dis", "#lb__zhenjin_hp" }
    local audio = table.indexOf(list, self.interaction.data)
    if audio == -1 then
      audio = 3
    else
      audio = audio == 1 and 2 or 1
    end
    room:notifySkillInvoked(player, skel.name, "offensive", use.tos)
    player:broadcastSkillInvoke(skel.name, audio)
    if not player.dead and self.interaction.data ~= "#lb__zhenjin_hp" then
      room:throwCard(use.card.fake_subcards, skel.name, player)
      table.remove(list, 1)
    end
    if not player.dead and self.interaction.data ~= "#lb__zhenjin_dis" then
      room:loseHp(player, 1, skel.name)
      table.removeOne(list, "#lb__zhenjin_hp")
    end
    if not player.dead and #list == 0 then
      local num = #table.filter(room:getOtherPlayers(player), function(to)
        return to.hp < player.hp
      end)
      room:addPlayerMark(player, "@lb__douzhi", num)
    end
    use.extra_data = use.extra_data or {}
    use.extra_data.lb__zhenjin_choices = list
  end,
  enabled_at_response = Util.FalseFunc,
})

skel:addEffect(fk.TargetConfirmed, {
  can_refresh = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.lb__zhenjin_list = data.extra_data.lb__zhenjin_list or {}
    return target == player and data.extra_data and data.extra_data.lb__zhenjin_choices and
    not table.contains(data.extra_data.lb__zhenjin_list, target.id)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    table.insert(data.extra_data.lb__zhenjin_list, target.id)
    local choices = data.extra_data.lb__zhenjin_choices
    if #choices > 0 then
      if table.contains(choices, "#lb__zhenjin_hp") then
        if room:askToSkillInvoke(target, { skill_name = skel.name, prompt = "#lb__zhenjin_lose:::" .. data.card:toLogString() }) then
          room:loseHp(target, 1, skel.name)
          return
        end
      else
        if #room:askToDiscard(target, {
              min_num = 1,
              max_num = 1,
              skill_name = skel.name,
              pattern = ".|.|.|.|.|equip",
              cancelable = true,
              prompt = "#lb__zhenjin_equip:::" .. data.card:toLogString(),
            }) > 0 then
          return
        end
      end
    end
    data.use.additionalEffect = (data.use.additionalEffect or 0) + 1
  end,
})

return skel
